package com.breaktian.demoapp.ui;

import android.location.Location;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.breaktian.assemble.adapter.PermissionAdapter;
import com.breaktian.assemble.provider.LocationProvider;
import com.breaktian.assemble.schedule.CommonTask;
import com.breaktian.assemble.schedule.ScheduleUtils;
import com.breaktian.assemble.utils.LocationUtil;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.demoapp.R;
import com.breaktian.demoapp.provider.BaseProvider;
import com.breaktian.demoapp.toobar.ToolBarBuilder;
import com.breaktian.demoapp.ui.base.BaseActivity;
import com.breaktian.demoapp.utils.ToastUtils;

import java.util.UUID;

public class TestGpsActivity extends BaseActivity {

    private TextView tv_gps;
    private LocationProvider locationProvider;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_gps);

        tv_gps = (TextView)findViewById(R.id.tv_gps);
        locationProvider = new LocationProvider(this);

        toolbar = (Toolbar)findViewById(R.id.toolBar);
        new ToolBarBuilder().setToolbar(toolbar).setTitle("测试gps").hideBackIcon(false)
                .build(this);

//        JumpManager.get().setActivity(this).putExtra("title","eee").jump("http://www.baidu.com").commit();
//        JumpManager.get().setActivity(this).jump("demo://TestJumpManagerActivity").commit();

        //开启一个定时任务
        int taskId = UUID.randomUUID().hashCode();
        ScheduleUtils.get().startSchedule(new CommonTask(taskId) {
            @Override
            protected void doTask() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPermissionAdapter.requestLocationPermission(new PermissionAdapter.PermissionVerifyCallBack() {
                            @Override
                            public void onPermissionGet() {
                                Location location = locationProvider.getLocation();
                                if(location!=null){
                                    String ss  = "onLocationChanged... location \r\n Latitude:"+location.getLatitude()+"\r\n Longitude:"+location.getLongitude();
                                    LogUtil.d("ss:"+ss);
                                    LogUtil.d("ss:"+location.getLatitude()+","+location.getLongitude());
                                    double[] dd = LocationUtil.standardToChina(location.getLatitude(), location.getLongitude());
                                    if(dd!=null&&dd.length==2){
                                        LogUtil.d("ss火星坐标:"+dd[0]+","+dd[1]);
                                    }
                                    tv_gps.setText(ss);
                                }
                            }

                            @Override
                            public void onPermissionLost() {
                                ToastUtils.show("请手动打开位置权限");
                            }
                        });

                    }
                });
            }
        },3000);
    }

    @Override
    protected BaseProvider getProvider() {
        return null;
    }

    @Override
    protected void onStart() {
        super.onStart();
        locationProvider.startLoadLocation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationProvider.stop();
    }
}
